﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace SaveTheQueen.Utils
{
    public class MouseInput
    {
        private static MouseState lastState = Mouse.GetState();
        private static MouseState actualState = Mouse.GetState();
        private static bool lockInput;
        private static bool centerMouse;

        public static void Pool()
        {
            lastState = actualState;
            actualState = Mouse.GetState();
            if (centerMouse)
                Mouse.SetPosition((int)(Core.ScreenX / 2f), (int)(Core.ScreenY / 2f));
        }

        public static void LockInput(bool lockState)
        {
            lockInput = lockState;
        }
        /// <summary>
        /// Set mouse position to the center of the screen each frame.
        /// <remarks>
        /// It is used to ensure that every frame the mouse will be moveable to all directions.
        /// If set to false, there are frames that it may be on the edge of the screen, making some movements impossible due to the limit of the screen.</remarks>
        /// </summary>
        /// <param name="centerState"></param>
        public static void CenterCursor(bool centerState)
        {
            centerMouse = centerState;
        }

        private static Button CheckButton(ButtonState actual, ButtonState last)
        {
            if (!lockInput)
            {
                if (actual == ButtonState.Pressed && actual != last)
                    return Button.Down; // if actual is pressed, but last is not.
                else if (actual == ButtonState.Pressed && actual == last)
                    return Button.Held; // if actual and last is pressed
                else // if actual is released
                    return Button.Up;
            }
            else // if input is locked
                return Button.Up;
        }

        public static Button Left
        {
            get { return CheckButton(actualState.LeftButton, lastState.LeftButton); }
        }
        public static Button Right
        {
            get { return CheckButton(actualState.RightButton, lastState.RightButton); }
        }
        public static Button Middle
        {
            get { return CheckButton(actualState.MiddleButton, lastState.MiddleButton); }
        }
        public static Button Additional1
        {
            get { return CheckButton(actualState.XButton1, lastState.XButton1); }
        }
        public static Button Additional2
        {
            get { return CheckButton(actualState.XButton2, lastState.XButton2); }
        }

        public static Vector2 Position
        {
            get { return new Vector2(actualState.X, actualState.Y); }
        }
        public static int X
        {
            get { return actualState.X; }
        }
        public static int Y
        {
            get { return actualState.Y; }
        }

        /// <summary>
        /// Variation in the position of the mouse since last frame.
        /// <remarks>Actual position - Last position</remarks>
        /// </summary>
        public static Vector2 Displace
        {
            get { return new Vector2(actualState.X - lastState.X, actualState.Y - lastState.Y); }
        }
    }
}
